package com.swq.java.code.chapter15.code06;

import com.swq.java.code.chapter15.code03.Customer;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class CustomerComparator implements Comparator<Customer> {
    public int compare(Customer c1, Customer c2) {

        if (c1.getName().compareTo(c2.getName()) > 0) return -1;
        if (c1.getName().compareTo(c2.getName()) < 0) return 1;

        return 0;
    }

    public static void main(String args[]) {
        Set<Customer> set = new TreeSet<Customer>(new CustomerComparator());

        Customer customer1 = new Customer("Tom", 15);
        Customer customer3 = new Customer("Jack", 16);
        Customer customer2 = new Customer("Mike", 26);
        set.add(customer1);
        set.add(customer2);
        set.add(customer3);

        for (Customer customer : set)
            System.out.println(customer.getName() + " " + customer.getAge());
    }
}